
use "$project_path/data/4_regdata/regdata", clear

log using "$project_path/output/tables/Table_2.smcl", replace

// Column 1 (Full sample)
rdrobust diff_m1_chix_econ runvar, all
rdrobust std_diff_m1_growth_gdpc_penn runvar, all
rdrobust mstd_diff_m1_inflation_imf runvar, all
rdrobust mstd_diff_m1_unemployment_ilo runvar, all
rdrobust std_diff_m1_trade_wb runvar, all
rdrobust std_diff_m1_hdi runvar, all
rdrobust std_diff_m1_democracy_index runvar, all
rdrobust diff_m1_general_chix runvar, all

// Column 2 (Presidential elections)
rdrobust diff_m1_chix_econ runvar if Type_Election=="Presidential", all
rdrobust std_diff_m1_growth_gdpc_penn runvar if Type_Election=="Presidential", all
rdrobust mstd_diff_m1_inflation_imf runvar if Type_Election=="Presidential", all
rdrobust mstd_diff_m1_unemployment_ilo runvar if Type_Election=="Presidential", all
rdrobust std_diff_m1_trade_wb runvar if Type_Election=="Presidential", all
rdrobust std_diff_m1_hdi runvar if Type_Election=="Presidential", all
rdrobust std_diff_m1_democracy_index runvar if Type_Election=="Presidential", all
rdrobust diff_m1_general_chix runvar if Type_Election=="Presidential", all

// Column 3 (Parliamentary elections)
rdrobust diff_m1_chix_econ runvar if Type_Election=="Parliamentary", all
rdrobust std_diff_m1_growth_gdpc_penn runvar if Type_Election=="Parliamentary", all
rdrobust mstd_diff_m1_inflation_imf runvar if Type_Election=="Parliamentary", all
rdrobust mstd_diff_m1_unemployment_ilo runvar if Type_Election=="Parliamentary", all
rdrobust std_diff_m1_trade_wb runvar if Type_Election=="Parliamentary", all
rdrobust std_diff_m1_hdi runvar if Type_Election=="Parliamentary", all
rdrobust std_diff_m1_democracy_index runvar if Type_Election=="Parliamentary", all
rdrobust diff_m1_general_chix runvar if Type_Election=="Parliamentary", all

// Column 4 (Incumbent on ballot)
rdrobust diff_m1_chix_econ runvar if subsample_incballot==1, all
rdrobust std_diff_m1_growth_gdpc_penn runvar if subsample_incballot==1, all
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_incballot==1, all
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_incballot==1, all
rdrobust std_diff_m1_trade_wb runvar if subsample_incballot==1, all
rdrobust std_diff_m1_hdi runvar if subsample_incballot==1, all
rdrobust std_diff_m1_democracy_index runvar if subsample_incballot==1, all
rdrobust diff_m1_general_chix runvar if subsample_incballot==1, all

// Column 5 (Incumbent not on ballot)
rdrobust diff_m1_chix_econ runvar if subsample_not_incballot==1, all
rdrobust std_diff_m1_growth_gdpc_penn runvar if subsample_not_incballot==1, all
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_not_incballot==1, all
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_not_incballot==1, all
rdrobust std_diff_m1_trade_wb runvar if subsample_not_incballot==1, all
rdrobust std_diff_m1_hdi runvar if subsample_not_incballot==1, all
rdrobust std_diff_m1_democracy_index runvar if subsample_not_incballot==1, all
rdrobust diff_m1_general_chix runvar if subsample_not_incballot==1, all

// Column 6 (High checks and balances)
rdrobust diff_m1_chix_econ runvar if subsample_high_checks==1, all
rdrobust std_diff_m1_growth_gdpc_penn runvar if subsample_high_checks==1, all
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_high_checks==1, all
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_high_checks==1, all
rdrobust std_diff_m1_trade_wb runvar if subsample_high_checks==1, all
rdrobust std_diff_m1_hdi runvar if subsample_high_checks==1, all
rdrobust std_diff_m1_democracy_index runvar if subsample_high_checks==1, all
rdrobust diff_m1_general_chix runvar if subsample_high_checks==1, all

// Column 6 (Low checks and balances)
rdrobust diff_m1_chix_econ runvar if subsample_low_checks==1, all
rdrobust std_diff_m1_growth_gdpc_penn runvar if subsample_low_checks==1, all
rdrobust mstd_diff_m1_inflation_imf runvar if subsample_low_checks==1, all
rdrobust mstd_diff_m1_unemployment_ilo runvar if subsample_low_checks==1, all
rdrobust std_diff_m1_trade_wb runvar if subsample_low_checks==1, all
rdrobust std_diff_m1_hdi runvar if subsample_low_checks==1, all
rdrobust std_diff_m1_democracy_index runvar if subsample_low_checks==1, all
rdrobust diff_m1_general_chix runvar if subsample_low_checks==1, all

log close

** Building .tex table

// Regressions 
#delimit ;
local outcomes  
		diff_m1_chix_econ
		std_diff_m1_growth_gdpc_penn
		mstd_diff_m1_inflation_imf
		mstd_diff_m1_unemployment_ilo
		std_diff_m1_trade_wb
		std_diff_m1_hdi 
		std_diff_m1_democracy_index
		diff_m1_general_chix
;
#delimit cr		

local subsample_count 7

// Initialize matrix of results and define value labels
local ncols = `subsample_count'*2
local nrows = 17
//Prepare matrix of coefficients and other statistics
mat Coefs = J(`nrows', `ncols', .)

mat list Coefs
	
local type_election Presidential Parliamentary
local subsamples "_incballot _not_incballot _high_checks _low_checks"

//inizialize indices
local row = 1
local next_row = `row' + 1

foreach v in `outcomes'{
	//Initialize indices
	local col = 1
	local pval_col = `subsample_count' + 1

	rdrobust `v' runvar, all
	
	//Store estimates and stats
	local b_`col': di %5.3f e(tau_cl)
	local se_`col': di %5.3f e(se_tau_rb)
	local p_`col': di %5.3f e(pv_rb)
	local N_`col': di %6.0f e(N)

	mat Coefs[`row',`col'] = `b_`col''
	mat Coefs[`next_row',`col'] = `se_`col''
	mat Coefs[`row',`pval_col'] = `p_`col''	
	
	foreach type in `type_election'{
		
		local ++col	
		local ++pval_col
		rdrobust `v' runvar if Type_Election == "`type'", all
		
		//Store estimates and stats
		local b_`col': di %5.3f e(tau_cl)
		local se_`col': di %5.3f e(se_tau_rb)
		local p_`col': di %5.3f e(pv_rb)
		local N_`col': di %6.0f e(N)
	
		mat Coefs[`row',`col'] = `b_`col''
		mat Coefs[`next_row',`col'] = `se_`col''
		mat Coefs[`row',`pval_col'] = `p_`col''	

	}
	
	foreach sub in `subsamples'{
		
		local ++col	
		local ++pval_col
		rdrobust `v' runvar if subsample`sub' == 1, all
		
		//Store estimates and stats
		local b_`col': di %5.3f e(tau_cl)
		local se_`col': di %5.3f e(se_tau_rb)
		local p_`col': di %5.3f e(pv_rb)
		local N_`col': di %6.0f e(N)
	
		mat Coefs[`row',`col'] = `b_`col''
		mat Coefs[`next_row',`col'] = `se_`col''
		mat Coefs[`row',`pval_col'] = `p_`col''	
		
	}
	
	local row = `row' + 2
	local next_row = `row' + 1
}
forvalues i=1/7{
	mat Coefs[17,`i'] = `N_`i'' // we use the fact that the regression for the general index is done last
}

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	gen varname = ""
	gen temp = _n
	replace varname = "\textbf{Economic performance}" if temp == 1
	replace varname = "SE_1" if temp == 2
	replace varname = "GDP p.c. growth" if temp == 3
	replace varname = "SE_2" if temp == 4
	replace varname = "(Minus) Inflation" if temp == 5
	replace varname = "SE_3" if temp == 6
	replace varname = "(Minus) Unemployment" if temp == 7
	replace varname = "SE_4" if temp == 8
	replace varname = "Trade" if temp == 9
	replace varname = "SE_5" if temp == 10
	replace varname = "\textbf{HDI}" if temp == 11
	replace varname = "SE_6" if temp == 12
	replace varname = "\textbf{Democracy}" if temp == 13
	replace varname = "SE_7" if temp == 14
	replace varname = "\textbf{General index}" if temp == 15
	replace varname = "SE_8" if temp == 16
	replace varname = "N" if temp == 17
	
	order varname, first
	drop temp

	//Compute equality test for the general idnex and store p-values
	sum Coefs2 if varname == "\textbf{General index}"
	local est_subsample_pres: di %5.3f r(mean)
	sum Coefs2 if varname == "SE_8"
	local se_subsample_pres: di %5.3f r(mean)
	sum Coefs3 if varname == "\textbf{General index}"
	local est_subsample_parl: di %5.3f r(mean)
	sum Coefs3 if varname == "SE_8"
	local se_subsample_parl: di %5.3f r(mean)

	sum Coefs4 if varname == "\textbf{General index}"
	local est_subsample_incballot: di %5.3f r(mean)
	sum Coefs4 if varname == "SE_8"
	local se_subsample_incballot: di %5.3f r(mean)
	sum Coefs5 if varname == "\textbf{General index}"
	local est_subsample_not_incballot: di %5.3f r(mean)
	sum Coefs5 if varname == "SE_8"
	local se_subsample_not_incballot: di %5.3f r(mean)

	sum Coefs6 if varname == "\textbf{General index}"
	local est_subsample_high: di %5.3f r(mean)
	sum Coefs6 if varname == "SE_8"
	local se_subsample_high: di %5.3f r(mean)
	sum Coefs7 if varname == "\textbf{General index}"
	local est_subsample_low: di %5.3f r(mean)
	sum Coefs7 if varname == "SE_8"
	local se_subsample_low: di %5.3f r(mean)
	
	
	local z = (`est_subsample_pres'-`est_subsample_parl')/sqrt((`se_subsample_pres')^2 + (`se_subsample_parl')^2)
	local p = string(2*(1-normal(abs(`z'))))
	local general_chix_eq_pval_pres_parl: display %04.3f `p'
	
	local z = (`est_subsample_incballot'-`est_subsample_not_incballot')/sqrt((`se_subsample_incballot')^2 + (`se_subsample_not_incballot')^2)
	local p = string(2*(1-normal(abs(`z'))))
	local general_chix_eq_pval_incballot: display %04.3f `p'

	local z = (`est_subsample_high'-`est_subsample_low')/sqrt((`se_subsample_high')^2 + (`se_subsample_low')^2)
	local p = string(2*(1-normal(abs(`z'))))
	local general_chix_eq_pval_checks: display %04.3f `p'
	  
	//Adjust the table before exporting
	replace varname = "" if strpos(varname, "SE_")>0
	order varname
	
	// Rename cols and labels
	forvalues n = 1/`subsample_count'{
		rename Coefs`n' Model`n'
	}
	//Cols where we store pvalues
	local n_min = `subsample_count' + 1
	local n_max = `subsample_count'*2
	local i = 1
	forvalues n = `n_min'/`n_max'{
		rename Coefs`n' P_Model`i'
		local ++i
	}
	
	label var varname ""
	label var Model1 "Baseline"
	label var Model2 "Pres."
	label var Model3 "Parl."
	label var Model4 "Yes"
	label var Model5 "No"
	label var Model6 "High"
	label var Model7 "Low"
	
		
	// Convert to string and add options
	forvalues i = 1/`subsample_count'{
		tostring Model`i', replace force format(%04.3f)
		replace Model`i'= "" if Model`i'=="."
		//Add parenthesis to SE and P-val
		replace Model`i' = "(" + Model`i' + ")" if missing(varname) & Model`i' !=""	
		//Remove decimals to N obs
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N"
	}
	
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	forvalues i = 1/`subsample_count'{
		
		replace Model`i' = Model`i' + "***" if P_Model`i' < `3_star'
		replace Model`i' = Model`i' + "**" if P_Model`i' < `2_star' & P_Model`i' > `3_star'
		replace Model`i' = Model`i' + "*" if P_Model`i' < `1_star' & P_Model`i' > `2_star'
		
		drop P_Model`i'
	}
	
	foreach var of varlist _all {
	qui replace `var' = ustrtrim(`var')	
}
	local title = "Heterogeneity analysis"
	local label_key "tab:baseline_het_pres_parl_ballot"
	local output "$project_path/output/tables/Table_2.tex"
	local headerlines " & & \multicolumn{2}{c}{\textbf{Election type}} & \multicolumn{2}{c}{\textbf{Incumbent on ballot}} & \multicolumn{2}{c}{\textbf{Checks \& balances}}"
	local footnote = "\caption*{\footnotesize \emph{Notes}: This table reports estimated effects of electoral turnovers for different subsamples. Each estimate corresponds to a separate regression. Column (1) reports results for the full sample. Columns (2) and (3) report results for the subsamples of presidential and parliamentary elections, respectively. Column (4) (resp., 5) shows results on the subsample of elections in which the candidate of the incumbency was the incumbent leader themselves (resp., someone else). Column (6) (resp., 7) shows results for the subsample of elections for which checks and balances on the year before the election were above (resp., below) the median, computed among close elections (i.e., elections with a running variable below 15pp in absolute value). The subsamples of columns (4) and (5) only include elections leading to the designation of a leader in the executive branch. Data Appendix \ref{app:party_leaders} explains how we identify the candidate of the incumbency. Checks and balances are measured as the average of two V-Dem indices: the judicial constraints on the executive index and the legislative constraints on the executive index. Using the method of \cite{clogg1995statistical}, we cannot reject the equality of the estimates for the general index for presidential and parliamentary elections (p-val. = `general_chix_eq_pval_pres_parl'), for the presence or absence of the incumbent leader on the ballot (p-val. = `general_chix_eq_pval_incballot'), and for high and low checks and balances (p-val. = `general_chix_eq_pval_checks'). We obtain broadly consistent results when running a parametric regression in which we include the interaction between the treatment and the dimension of heterogeneity. We report the number of observations in the regressions of the table's last row, where the outcome is the general index of country performance. $^{*} p<0.10,^{**} p<0.05,^{***} p<0.01$.}"

	

	texsave using "`output'", replace title("`title'") varlabels noendash hlines(16) ///
							  footnote("`footnote'") ///
							  headerlines("`headerlines'" "& (1) & (2) & (3) & (4) & (5) & (6) & (7)" ) ///
							  frag nofix marker("`label_key'") location(H)
	
	
